package ml.exp;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.Surface;


import java.io.IOException;


public class Media {
    public enum ResMode {
        Close,
        FromAssets,
        FromUrl,
    }

    public enum PrepareState {
        None,
        Prepare,
        PrepareSucc,
        PrepareFail,
    }

    Context _root;

    MediaPlayer _player;

    PrepareState _video_preparestate;
    int _video_width;
    int _video_height;
    int _video_duration;
    ResMode _urlmode;

    AssetFileDescriptor _urlafd;
    float _volume;
    boolean _loop;
    boolean _pause;
    boolean _isplay;

    int _id;
    static int g_vedioid = 0;

    public Media(Context context) {
        _player = null;
        _video_preparestate = PrepareState.None;
        _video_width = 0;
        _video_height = 0;
        _video_duration = 0;

        _loop = false;
        _isplay = false;
        _volume = 1.0f;
        _pause = false;
        _urlmode = ResMode.Close;
        _urlafd = null;

        InitTextureSurfaceVars();

        g_vedioid++;
        this._id = g_vedioid;

        this._root = context;
        _player = new MediaPlayer();
    }

    public int GetID() {
        return _id;
    }

    //Init Release

    //Open Close
    //Start Stop
    //三对操作

    //播放流程 New Open Start
    //停止流程 Stop Close Release


    public void Release() {
        this.Close();

        _player = null;

    }

    public void Pause() {

        _pause = true;
        this._player.pause();

    }

    public void Resume() {

        _pause = false;
        if (this._isplay) {
            this._player.start();
        }
    }


    public float GetVolume() {
        return _volume;
    }

    public boolean GetLoop() {
        return _loop;
    }

    public void SetVolume(float v) {
        this._volume = v;
    }

    public void SetLoop(boolean v) {
        this._loop = v;
    }

    public boolean HasInit() {
        return this._player != null;
    }


    public void Open(ResMode mode, String name, boolean autoplay) {
        this._player.setLooping(this._loop);
        this._player.setVolume(_volume, _volume);
        this._urlmode = mode;
        if (mode == ResMode.FromAssets) {

            try {

                AssetManager am = this._root.getAssets();
                _urlafd = am.openFd(name);

                _player.setDataSource(_urlafd);
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("==E== Open Fail FromAssets:"+name , e.getMessage());
                this._video_preparestate = PrepareState.PrepareFail;
                return;
            }
        } else if (mode == ResMode.FromUrl) {
            try {
                _player.setDataSource(name);
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("==E== Open Fail FromUrl:"+name, e.getMessage());
                this._video_preparestate = PrepareState.PrepareFail;
                return;
            }
        }
        //player.prepareAsync();//准备播放

        _player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                int w = mediaPlayer.getVideoWidth();
                int h = mediaPlayer.getVideoHeight();
                int d = mediaPlayer.getDuration();

                Log.e("==E==", "video size=" + w + "," + h + " dur=" + d);


                _video_width = w;
                _video_height = h;
                _video_duration = d;
                _video_preparestate = PrepareState.PrepareSucc;
                if (autoplay && !_pause) {
                    Start();

                }
            }
        });
        this._video_preparestate = PrepareState.Prepare;
        _player.prepareAsync();//异步Prepare


    }

    public void Close() {
        this.Stop();
        if (this._urlmode == ResMode.FromAssets)
            try {
                _urlafd.close();
            } catch (IOException e) {

            }
        _urlafd = null;
        this._urlmode = ResMode.Close;
    }

    public void Start() {
        if (_video_preparestate != PrepareState.PrepareSucc) {
            Log.w("==E==", "Video Play NotPrepared.");
            return;
        }
        _player.seekTo(0);
        _player.start();
        _isplay = true;
        _pause = false;

        Log.w("==E==", "Video Play");
    }

    public void Stop() {
        if (_video_preparestate != PrepareState.PrepareSucc) {
            Log.w("==E==", "Video Play NotPrepared.");
            return;
        }
        _player.stop();
        _isplay = false;


    }

    public PrepareState GetPrepareState() {
        return this._video_preparestate;
    }

    public void WaitPrepare() {
        while (this._video_width <= 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {

            }
        }
    }

    public boolean IsPrepare() {
        return _video_preparestate == PrepareState.PrepareSucc;
    }

    public int GetVideoWidth() {
        return this._video_width;
    }

    public int GetVideoHeight() {
        return this._video_height;
    }

    public int GetVideoDuration() {
        return this._video_duration;
    }

    public void SetSurface(Surface surface) {
        this._player.setSurface(surface);
    }



    boolean _texdone;


    void InitTextureSurfaceVars() {

        _texdone = false;

    }


    public void SetTextureSurface(SurfaceTexture texture) {


//        long threadid = Thread.currentThread().getId();
//        Log.w("==E==", "SetTextureSurface thread=" + threadid);

        this._player.setSurface(new Surface(texture));



    }



}
